package com.xue.action;

import java.io.InputStream;
import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

/**
 * 
 * @author 北京八维博大科技 薛建新
 * @date 2022年7月7日 下午2:35:03
 * @Copyright 北京八维博大科技
 */
public final class SslTool {
	public static final SSLContext getSSLContext(String fileName) throws Exception {
		// load up the key store
		// 客户端只需要公钥，或从浏览器下载公钥
		String STORETYPE = "JKS";
		String STOREPASSWORD = "654321";
		String KEYPASSWORD = "654321";

		KeyStore ks = KeyStore.getInstance(STORETYPE);
		InputStream is = SSLTest.class.getResourceAsStream(fileName);
		ks.load(is, STOREPASSWORD.toCharArray());

		KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
		kmf.init(ks, KEYPASSWORD.toCharArray());
		TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
		tmf.init(ks);

		SSLContext sslContext = null;
		sslContext = SSLContext.getInstance("TLSv1.2");//TLSv1.2 或TLS
		sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
		return sslContext;
	}
}
